<?php
declare (strict_types = 1);

namespace app\controller;
use app\BaseController;
use app\common\ApiResponse;
use \app\service\AccountBookService;

class AccountBookController  extends BaseController
{
    private $accountBookService;

    public function __construct()
    {
        $app = app(); // 获取当前应用的实例
        $this->accountBookService = new AccountBookService($app);
    }
    /**
     * 查询账本列表
     *
     * @return \think\Response
     */
    public function getList()
    {
        $data = input();
        if(empty($data['user_id'])){
            return json("缺少参数user_id");
        }
        $list = $this->accountBookService->getList($data);
        return ApiResponse::successList($list, "查询成功");
    }

    /**
     * 添加账本
     *
     * @return \think\Response
     */
    public function create()
    {
        $data = input();
        if(empty($data['acc_name'] ) || empty($data['user_id'])){
            return json("缺少参数acc_name|user_id");
        }
        $res = $this->accountBookService->create($data);
        return ApiResponse::success($res, "添加成功");
    }

    /**
     * 修改账本
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function update()
    {
        $data = input();
        if(empty($data['acc_id'] ) || empty($data['acc_name'] ) || empty($data['user_id'])){
            return json("缺少参数acc_id|acc_name|user_id");
        }
        $res = $this->accountBookService->update($data);
        return ApiResponse::success($res, "修改成功");
    }

    /**
     * 查询账本
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function getOne()
    {
        $data = input();
        if(empty($data['acc_id'] )){
            return json("缺少参数acc_id");
        }
        $res = $this->accountBookService->getOne($data);
        return ApiResponse::success($res, "查询成功");
    }

    /**
     * 删除账本
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete()
    {
        $data = input();
        if(empty($data['acc_id'] )){
            return json("缺少参数acc_id");
        }
        $res = $this->accountBookService->delete($data);
        return ApiResponse::success($res, "删除成功");

    }
}
